package com.kefu.system.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kefu.model.SystemUser;
import com.kefu.system.services.SystemServices;
import com.kefu.util.Time;

@Controller
@Scope("prototype")
@RequestMapping("/system")
public class SystenUserController {
	public SystenUserController() {
		System.out.println("Info:init system user register controller...   Time:"+Time.getNowTime());
	}

	@Autowired
	SystemServices systemServices;
	@RequestMapping("/register.c")
	public String register(String name,String greetings,HttpServletResponse response) throws IOException{
		//生成客户端编号
		String user_uuid=java.util.UUID.randomUUID().toString();
		SystemUser su=new SystemUser();
		su.setUuid(user_uuid);
		su.setName(name);
		su.setState("hide");
		su.setCreate_time(Time.getNowTime());
		su.setGreetings(greetings!=null&&greetings.trim().equals("")?null:greetings);
		su.setCustomer(0);
		if(name==null||name.trim().equals("")){
			return "redirect:/create.jsp?err=noName";
		}else if(!systemServices.checkName(name)){
			return "redirect:/create.jsp?err=nameTwo";
		}else if(systemServices.insert(su)==1){
			return "redirect:/manager.jsp";
		}else{
			return  "redirect:/create.jsp?err=createErr";
		}
	}
	@RequestMapping("/load.c")
	public void load(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("loaded",true);
		List<SystemUser> user_list=systemServices.findAll();
		request.setAttribute("user_list",user_list);
		request.getRequestDispatcher("/manager.jsp").forward(request, response);
	}
}
